<?php echo view('header.html');?>

<body class="layui-view-body">
    <div class="layui-content">
        <div class="layui-row">

            <div class="layui-card">
                <div class="layui-card-header">搜索参数</div>
                <div class="layui-card-body">
                    <form class="layui-form" id="searchForm">
                        
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <div class="layui-form-mid">权限名称:</div>
                                <div class="layui-input-inline">
                                    <input type="text" name="name" autocomplete="off" class="layui-input">
                                </div>

                                <div class="layui-form-mid">上级:</div>
                                <div class="layui-input-inline">
                                    <select name="pid">
                                        <option value=""> 所有</option>
                                        <?php if($parent):?>
                                        <?php foreach($parent as $v):?>
                                        <option value="<?=$v['id']?>"><?=$v['spacer']?><?=$v['name']?></option>
                                        <?php endforeach?>
                                        <?php endif?>
                                    </select>
                                </div>

                                <button class="layui-btn layui-btn-blue" type="button" lay-submit lay-filter="ams_search" id="ams_search">查询</button>
                                <button class="layui-btn layui-btn-primary" type="button" lay-submit lay-filter="ams_reset" id="ams_reset">重置</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

            <div class="layui-card">
                <div class="layui-card-header">
                    权限规则管理
                </div>
                <div class="layui-card-body">
                    <table id="listTable" lay-filter="listTable"></table>
                    <script type="text/html" id="toolbarOption">
                        <button type="button" lay-event="toolbar_del" class="layui-btn layui-btn-danger layui-btn-sm">删除</button>
                        <button type="button" class="layui-btn layui-btn-blue layui-btn-sm" lay-event="rule_add">添加顶级/上级</button>
                    </script>

                    <script type="text/html" id="statusTpl">
                        <input type="checkbox" name="status" value="{{d.status}}" lay-skin="switch" lay-text="显示|不显示" lay-filter="statusSwitch" {{ d.status == 1 ? 'checked' : '' }}>
                    </script>

                    <script type="text/html" id="barOption">
                        <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="edit">修改</a>
                        {{# if (d.level<=2){ }}
                        <a class="layui-btn layui-btn-xs layui-btn-default" lay-event="add">添加下级</a>
                        {{# } }}
                        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
    <?php echo view('footer.html');?>
    <script type="text/javascript">
        layui.use(['form', 'jquery', 'layer', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                layer = layui.layer,
                table = layui.table;
            

            /**index**/

            //数据初始化
            var tableInit = table.render({
                elem: '#listTable',
                toolbar: "#toolbarOption",
                defaultToolbar: [],
                title: '规则列表',
                url: adminpath + 'admin/rbac/rule',
                method: 'post',
                cols: [
                    [
                        { type: 'checkbox' },
                        { field: 'id', title: 'ID', width: 80, unresize: true, sort: true },
                        {
                            field: 'name', title: '名称', templet: function (d) {
                                return d.spacer + d.name
                            }
                        },
                        { field: 'uri', title: 'URI' },
                        {
                            field: 'skip',
                            title: '是否验证',
                            templet: function (d) {
                                switch (d.skip) {
                                    case '1':
                                        return '<button class="layui-btn layui-btn-xs">不验证</button>';
                                        break;
                                    case '0':
                                        return '<button class="layui-btn layui-btn-danger layui-btn-xs">验证</button>';
                                        break;
                                }
                            }
                        },
                        { title: '操作', toolbar: '#barOption' }
                    ]
                ],
                limit: 1,
                limits: [1],
                page: true,
                response: {
                    statusCode: 0,
                    countName: 'count'
                },
                parseData: function (res) {
                    return {
                        'code': res.code,
                        'data': res.data.data,
                        'count': res.data.total
                    };
                }
            });


            //表头工具栏
            table.on('toolbar(listTable)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                var ids = [];
                $.each(data, function (i, v) {
                    ids.push(v.id);
                })

                switch (obj.event) {

                    case 'toolbar_del':
                        layer.confirm('确定要删除这些？', function (index) {
                            $.post(adminpath + 'admin/rbac/delRule', { ids: ids }, function (res) {
                                if (res.code == 0) {
                                    layer.msg(res.msg, { icon: 1, time: 1500 });
                                } else {
                                    layer.msg(res.msg, { icon: 5, anim: 6 });
                                }
                                tableInit.reload();
                            }, 'json');
                            layer.close(index);
                        });
                        break;

                    case 'rule_add':
                        // var title = '添加/修改规则';
                        // var url = adminpath + 'admin/rbac/addRule';
                        // open_parent_tab(title, url);

                        layer.open({
                            type: 2,
                            area: ['900px', '80%'],
                            title: '添加/修改权限规则',
                            content: [adminpath + 'admin/rbac/addRule']
                        });
                        break;

                }
                ;
            });

            window.displayOrder = function (id, obj) {
                var displayorder = $(obj).val();
                $.post('displayorder', { id: id, displayorder: displayorder }, function (res) {
                    console.log(res);
                    if (res.code == 0) {
                        layer.msg('排序成功', { time: 1000, icon: 1 });
                    } else {
                        layer.msg(res.msg, { time: 2000, icon: 5 });
                    }
                }, 'json');
            }

            //监听表格行操作
            table.on('tool(listTable)', function (obj) {
                var data = obj.data;
                var id = data.id;
                var pid = data.pid;

                switch (obj.event) {

                    case 'add':
                        layer.open({
                            type: 2,
                            area: ['900px', '80%'],
                            title: '添加规则',
                            content: [adminpath + 'admin/rbac/addRule?pid=' + id]
                        });
                        break;

                    case 'edit':

                        layer.open({
                            type: 2,
                            area: ['900px', '80%'],
                            title: '添加/修改规则',
                            content: [adminpath + 'admin/rbac/editRule?id=' + id]
                        });
                        break;

                    case 'del':
                        layer.confirm('确定要删除吗？', function (index) {
                            $.post(adminpath + 'admin/rbac/delRule', { ids: [id] }, function (res) {
                                if (res.code == 0) {
                                    tableInit.reload();
                                    layer.msg('操作成功', { icon: 1, time: 1000 });
                                } else {
                                    layer.msg(res.msg, { icon: 5, anim: 6, time: 2000 });
                                }
                            }, 'json');
                        });

                        break;
                    case 'listAuth':
                        var title = '权限管理';
                        var url = adminpath + 'admin/rbac/auth?roleid=' + id;
                        open_parent_tab(title, url);
                        break;
                    case 'listUser':
                        var title = '用户管理';
                        var url = adminpath + 'admin/rbac/index?roleid=' + id;
                        open_parent_tab(title, url);
                        break;
                }
            });

            //监听状态操作
            form.on('switch(statusSwitch)', function (obj) {
                var id = obj.othis.parents('tr').find("td:eq(1)").find(".layui-table-cell").text();
                var title = '';
                var val = '';
                if (this.value == 1) {
                    title = '您确定要[禁用]它吗?';
                    val = 0;
                } else {
                    title = '您确定要[启用]它吗?';
                    val = 1;
                }

                //点确认再切换
                var x = obj.elem.checked;
                obj.elem.checked = !x;
                form.render('checkbox');

                layer.confirm(title, function (index) {
                    $.post(adminpath + "admin/rbac/switchRuleStatus", { id: id, value: val }, function (res) {
                        if (res.code == 0) {
                            layer.msg('操作成功', { icon: 1, time: 1500 });
                        } else {
                            layer.msg(res.msg, { icon: 5, anim: 6 });
                        }
                        tableInit.reload();
                    }, 'json');
                    layer.close(index);
                });
            });

            //标题搜索
            form.on('submit(searchTitle)', function (data) {
                var value = data.field['title'];
                tableInit.reload({
                    where: { title: value },
                    page: {
                        curr: 1
                    }
                });
            });

            //搜索条件
            form.on('submit(ams_reset)', function (data) {
                $("#searchForm")[0].reset();
                form.render();
                $('#ams_search').trigger('click');
            });

            //标题搜索
            form.on('submit(ams_search)', function (data) {
                tableInit.reload({
                    where: data.field
                    , page: {
                        curr: 1
                    }
                });
            });
        });

    </script>
</body>

</html>
